/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.web.controller.api;

import javax.annotation.Resource;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yunji.framework_template.biz.service.NewsService;
import com.yunji.framework_template.orm.persistence.vo.DataDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * ClassName:SwaggerApiGenerateTestController <br/>
 * Date: 2018年10月17日 上午11:17:56 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
@Slf4j
@RestController
@Controller
@RequestMapping("/api/news")
@Api(value = "新闻接口", tags = "newsAPI", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class NewsApiController {

    @Resource
    private NewsService newsService;

    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "Long", name = "currentPage", value = "当前页的页码", required = true, defaultValue = "1") })
    @ApiOperation(value = "获取最新新闻的接口", notes = "如果分页数超过了总记录数，则不会返回数据", httpMethod = "GET")
    @RequestMapping(value = "/getLatestNews/{currentPage}", method = { RequestMethod.GET, RequestMethod.POST })
    public DataDTO<?> getLatestNews(@PathVariable("currentPage") long currentPage) {
        log.info("获取最新的新闻，当前页是:" + currentPage);
        try {
            return DataDTO.success(newsService.getPageNews(currentPage));
        } catch (Exception e) {
            log.error("Get latest news fail:" + e.getMessage(), e);
            return DataDTO.fail(500, "Get latest news fail.");
        }
    }

    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "Long", name = "currentPage", value = "当前页的页码", required = true, defaultValue = "1") })
    @ApiOperation(value = "获取最新新闻的接口", notes = "如果分页数超过了总记录数，则不会返回数据", httpMethod = "GET")
    @RequestMapping(value = "/pullLatestNews/{currentPage}/{currentPullPage}/{maxNewsTime}", method = { RequestMethod.GET,
                                                                                                        RequestMethod.POST })
    public DataDTO<?> pullLatestNews(@PathVariable("currentPage") long currentPage,
                                     @PathVariable("currentPullPage") long currentPullPage,
                                     @PathVariable("maxNewsTime") long maxNewsTime) {
        log.info("获取最新的新闻，当前页是:" + currentPage);
        try {            
            return DataDTO.success(newsService.getPageNews(currentPage));
        } catch (Exception e) {
            log.error("Get latest news fail:" + e.getMessage(), e);
            return DataDTO.fail(500, "Get latest news fail.");
        }
    }
}
